package com.dy.scs.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dy.scs.pojo.Course;
import com.dy.scs.pojo.Result;
import com.dy.scs.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * course表
 */
@Slf4j
@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    /**
     * 分页查询+模糊查询+排序
     * @param pages
     * @param pageSize
     * @return
     */
    @GetMapping("/pages")
    public Result pages(@RequestParam(defaultValue = "1") Integer pages,
                        @RequestParam(defaultValue = "10")Integer pageSize,
                        @RequestParam(required = false) String cname,
                        @RequestParam(required = false) String sortBy,
                        @RequestParam(required = false) String sortType){
        Page<Course> page=courseService.pages(pages,pageSize,cname,sortBy,sortType);
        return Result.success(page);
    }

    /**
     * 删除
     * @param courses
     * @return
     */
    @DeleteMapping("/delete")
    public Result deleteBy(@RequestParam List<String> courses){
        boolean b = courseService.removeBatchByIds(courses);
        if(!b){
            return Result.error("删除失败");
        }
        return Result.success();
    }

    /**
     * 更新
     * @param course
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Course course){
        boolean b = courseService.updateById(course);
        if(!b){
            return Result.error("输入不完整");
        }
        return Result.success();
    }

    /**
     * 新建
     * @param course
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody Course course){
        boolean save = courseService.save(course);
        if(!save){
            return Result.error("新建失败");
        }
        return Result.success();
    }

    /**
     * 根据cno查询
     * @param cno
     * @return
     */
    @GetMapping("/getBy")
    public Result getBycno(@PathVariable String cno){
        Course byId = courseService.getById(cno);
        if(byId==null){
            return Result.error("该学号为空");
        }
        return Result.success(byId);
    }


}
